﻿using Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

/// <summary>
/// methods 的摘要说明
/// </summary>
public class methods
{
    public methods()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }


    public static string getYOrderState(string OState)
    {
        string Result = "";
        int state = int.Parse(OState);
        switch (state)
        {
            case -3: Result = "<div style=\"padding: 1px 1px;font-size:12px;line-height:1.5;border - radius: 3px;background-color:#ccc; border-color:#ed5565;;color:#FFFFFF;text-align:center;\">作 废</div>"; break;
            case -2: Result = "<div style=\"padding: 1px 1px;font-size:12px;line-height:1.5;border - radius: 3px;background-color:#ccc; border-color:#ed5565;;color:#FFFFFF;text-align:center;\">已取消</div>"; break;
            case -1: Result = "<div style=\"padding: 1px 1px;font-size:12px;line-height:1.5;border - radius: 3px;background-color:#ed5565; border-color:#ed5565;;color:#FFFFFF;text-align:center;\">待确定</div>"; break;
            case 0: Result = "<div style=\"padding: 1px 1px;font-size:12px;line-height:1.5;border - radius: 3px;background-color:#3db934; border-color:#ed5565;;color:#FFFFFF;text-align:center;\">核对无误确认</div>"; break;

        }
        return Result;
    }


    public static string getSOrderState(string OState)
    {
        string Result = "";
        int state = int.Parse(OState);
        switch (state)
        {
            case -4: Result = "<div style=\"padding: 1px 1px;font-size:12px;line-height:1.5;border - radius: 3px;background-color:#333;; border-color:##ed5565;;color:#FFFFFF;text-align:center;\">系统作废</div>"; break;
            case -3: Result = "<div style=\"padding: 1px 1px;font-size:12px;line-height:1.5;border - radius: 3px;background-color:#333;; border-color:##ed5565;;color:#FFFFFF;text-align:center;\">后台作废</div>"; break;
            case -2: Result = "<div style=\"padding: 1px 1px;font-size:12px;line-height:1.5;border - radius: 3px;background-color:#333;; border-color:##ed5565;;color:#FFFFFF;text-align:center;\">用户取消</div>"; break;
            case 0: Result = "<div style=\"padding: 1px 1px;font-size:12px;line-height:1.5;border - radius: 3px;background-color:#333;; border-color:##ed5565;;color:#FFFFFF;text-align:center;\">待支付</div>"; break;
            case 1: Result = "<div style=\"padding: 1px 1px;font-size:12px;line-height:1.5;border - radius: 3px;background-color:#ed5565;; border-color:##ed5565;;color:#FFFFFF;text-align:center;\">待发货</div>"; break;
            case 2: Result = "<div style=\"padding:1px 1px;font-size:12px;line-height:1.5;border - radius: 3px;background-color: #23c6c8; border - color: #23c6c8;color: #FFFFFF;text-align:center;\">已发货</div>"; break;
            case 3: Result = "<div style=\"padding:1px 1px;font-size:12px;line-height:1.5;border - radius: 3px;background-color: #23c6c8; border - color: #23c6c8;color: #FFFFFF;text-align:center;\">已验收</div>"; break;
            case 4: Result = "<div style=\"padding:1px 1px;font-size:12px;line-height:1.5;border - radius: 3px;background-color: #23c6c8; border - color: #23c6c8;color: #FFFFFF;text-align:center;\">已评价</div>"; break;

        }
        return Result;
    }

    #region 根据快递公司编码，匹配快递公司名称 （快递100）
    public static string getkuaidiComNameByCode(string Code)
    {
        string Result = "";
        switch (Code)
        {
            case "yunda": Result = "韵达快递"; break;
            case "yuantong": Result = "圆通速递"; break;
            case "zhongtong": Result = "中通快递"; break;
            case "shunfeng": Result = "顺丰速运"; break;
            case "youzhengguonei": Result = "邮政快递"; break;
            case "huitongkuaidi": Result = "百世快递"; break;
            case "jd": Result = "京东物流"; break;
            case "shentong": Result = "申通快递"; break;
            case "tiantian": Result = "天天快递"; break;
            case "jtexpress": Result = "极兔速递"; break;
            case "ems": Result = "EMS"; break;
            case "youzhengbk": Result = "邮政标准"; break;
            case "zhongyouex": Result = "众邮快递"; break;
            case "debangwuliu": Result = "德邦"; break;
            case "zhaijisong": Result = "宅急送"; break;
            case "debangkuaidi": Result = "德邦快递"; break;
            case "zhongtongkuaiyun": Result = "中通快运"; break;
            case "youshuwuliu": Result = "优速快递"; break;
            case "baishiwuliu": Result = "百世快运"; break;
            case "yundakuaiyun": Result = "韵达快运"; break;
        }
        return Result;
    }
    #endregion

    #region 获取商品图片
    /// <summary>
    /// 获取第一张图片
    /// </summary>
    /// <param name="Img"></param>
    /// <returns></returns>
    public static List<string> GetImgs(string Img)
    {
        Uri uri = HttpContext.Current.Request.Url;
        string[] result = Img.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

        String host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;
        List<string> list = new List<string>();

        foreach (var item in result)
        {
            list.Add(host + item);
        }

        return list;

    }
    #endregion



    #region 获取商品图片
    /// <summary>
    /// 获取第一张大图片
    /// </summary>
    /// <param name="Img"></param>
    /// <returns></returns>
    public static string getFirstBiGImg(string Img)
    {
        string Result = "";
        Uri uri = HttpContext.Current.Request.Url;
        String host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;

        if (Img != null)
        {
            string[] imgArr = Img.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            if (imgArr.Length != 0)
                Result = host+imgArr[0].Replace("Up","upload");
        }
        return Result;
    }
    #endregion

    #region 获取第一张图片
    /// <summary>
    /// 获取第一张图片
    /// </summary>
    /// <param name="Img"></param>
    /// <returns></returns>
    public static string getFirstImg(string Img)
    {

        string Result = "";
        Uri uri = HttpContext.Current.Request.Url;
        String host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;

        if (Img != null)
        {
            string[] imgArr = Img.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            if (imgArr.Length != 0)
                Result = host + imgArr[0];
        }
        return Result;
    }


    public static string getFImg(string Img)
    {

        string Result = "";
     
        if (Img != null)
        {
            string[] imgArr = Img.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            if (imgArr.Length != 0)
                Result = imgArr[0];
        }
        return Result;
    }

    #endregion

    #region 订单号
    /// <summary>
    /// 订单号
    /// </summary>
    /// <param name="oprnId"></param>
    /// <returns></returns>
    public static string getOrderNumber(string guid)
    {
        string ordderNumber = "";
        Entities db = new Entities();
        DateTime today = DateTime.Now.Date;//今天
        #region 前4位
        //1,前4位:月日
        string frontFour = DateTime.Now.ToString("yyyyMMdd"); //今天
        #endregion
        #region 中4位
        //2,中4位：电话号码
        string middleFour = "9999";
        string num = "";
        var wx = db.User.SingleOrDefault(x => x.UGuid == guid);
        if (wx != null && wx.UTel != null)
        {
            num = methods.GetResultFromReg(wx.UTel, new Regex("^[0-9]+$"));
        }
        else
        {
            var Ouser = db.S_UADDRESS.FirstOrDefault(p => p.Uopenid == guid);
            if (Ouser != null)
                num = methods.GetResultFromReg(Ouser.UAPhone, new Regex("^[0-9]+$"));
        }
        if (num.Length >= 4)
            middleFour = num.Substring(num.Length - 4, 4);
        #endregion
        #region 后4位
        //3,后4位：今日第几条订单
        var Order = db.S_Order.Where(x => x.OCreatTime >= today).OrderByDescending(x => x.OCreatTime);
        if (Order.Count() == 0)
        {
            ordderNumber = frontFour + middleFour + "1001";
        }
        else
        {
            string munber = Order.FirstOrDefault().OOrderNumber;
            ordderNumber = frontFour + middleFour + (Convert.ToInt64(munber.Substring(munber.Length - (munber.Length - 12))) + 1).ToString();
        }
        #endregion
        ////2,中4位：电话号码
        ////20201017 17617  1001
        ////20201016 1016 1424
        //string middleFour = "9999";
        //var wx = db.User.SingleOrDefault(x => x.UGuid == guid);
        //if (wx != null && wx.UTel != null)
        //{
        //    middleFour = wx.UTel.Substring(7).Trim();
        //}
        //else
        //{
        //    var Ouser = db.S_UADDRESS.FirstOrDefault(p => p.Uopenid == guid);
        //    if (Ouser != null) { middleFour = Ouser.UAPhone.Substring(7).Trim(); }
        //}

        ////3,后4位：今日第几条订单
        //var Order = db.S_Order.Where(x => x.OCreatTime >= today).OrderByDescending(x => x.OCreatTime);
        //if (Order.Count() == 0)
        //{
        //    ordderNumber = frontFour + middleFour + "10001";
        //}
        //else
        //{
        //    string munber = Order.FirstOrDefault().OOrderNumber;
        //    //int backNumber = Convert.ToInt32(munber.Substring(13)) + 1;
        //    ordderNumber = (Convert.ToInt64(munber) + 1).ToString();
        //}

        return ordderNumber;
    }


    public static string getYOrderNumber(string openId)
    {
        string ordderNumber = "";
        Entities db = new Entities();
        DateTime today = DateTime.Now.Date;//今天

        //1,前4位:月日
        string frontFour = DateTime.Now.ToString("yyyyMMdd").Trim(); //今天

        //2,中4位：电话号码
        string middleFour = "9999";
        var wx = db.User.SingleOrDefault(x => x.UOpenID == openId);
        if (wx != null && wx.UTel != null)
        {
            middleFour = wx.UTel.Substring(7);
        }
        else
        {
            var Ouser = db.S_UADDRESS.FirstOrDefault(p => p.Uopenid == openId);
            if (Ouser != null) { middleFour = Ouser.UAPhone.Substring(7); }
        }

        //3,后4位：今日第几条订单
        var Order = db.Y_Order.Where(x => x.OCreatTime >= today).OrderByDescending(x => x.OCreatTime);
        if (Order.Count() == 0)
        {
            ordderNumber = frontFour + middleFour + "1001";
        }
        else
        {
            string munber = Order.FirstOrDefault().OOrderNumber;
            //int backNumber = Convert.ToInt32(munber.Substring(13)) + 1;
            ordderNumber = (Convert.ToInt64(munber) + 1).ToString();
        }

        return ordderNumber;
    }

    #endregion

    #region 星级
    /// <summary>
    /// 下级购买商品数量达到，或者充值金额达到，即可升级
    /// </summary>
    /// <param name="_OPenId"></param>
    /// <returns></returns>
    public static int getStars(string _OPenId)//,商品ID,购买者OpenId
    {
        int Result = 0;
        int OrderNum = 0;
        Entities MyDB = new Entities();
        var childs = from n in MyDB.User.Where(x => x.UparentId == _OPenId) select n;//机柜所有者 的 所有下级
        var CZ = MyDB.U_CZ.Where(x => x.UGuid == _OPenId && x.CZstat == 1 && x.CZPayType == "充值").Sum(x => x.CZMoney);
        if (CZ == null)
            CZ = 0;

        foreach (var child in childs)
        {
            OrderNum += Convert.ToInt32(MyDB.S_Order.Where(p => p.OopenID == child.UOpenID && p.OPayState == 1).Sum(p => p.ONumber));
        }
        if ((4050 < OrderNum) || (40050 <= Convert.ToDouble(CZ)))
        {
            Result = 5;
        }
        else if ((1350 < OrderNum && OrderNum <= 4050) || (13500 <= Convert.ToDouble(CZ) && Convert.ToDouble(CZ) < 40050))
        {
            Result = 4;
        }
        else if ((450 < OrderNum && OrderNum <= 1350) || (4500 <= Convert.ToDouble(CZ) && Convert.ToDouble(CZ) < 13500))
        {
            Result = 3;
        }
        else if ((150 < OrderNum && OrderNum <= 450) || (1500 <= Convert.ToDouble(CZ) && Convert.ToDouble(CZ) < 4500))
        {
            Result = 2;
        }
        else if ((50 < OrderNum && OrderNum <= 150) || (450 <= Convert.ToDouble(CZ) && Convert.ToDouble(CZ) < 1500))
        {
            Result = 1;
        }
        return Result;
    }
    #endregion

    #region  给机柜主收益
    /// <summary>
    /// 
    /// </summary>
    /// <param name="_OrderId"></param>
    /// <param name="_OPenId"></param>
    /// <returns></returns>
    public static string giveProfitToMachineOwner(int _OrderId, string _OPenId)//,商品ID,购买者OpenId
    {
        string Result = "";
        Entities MyDB = new Entities();
        ArrayList nextChilds = new ArrayList();//定义一个string的list，用来存放直接下一级OpenId
        var order = MyDB.S_Order.SingleOrDefault(p => p.OrderID == _OrderId);
        if (order != null)
        {
            var goods = MyDB.S_Goods.SingleOrDefault(p => p.GID == order.SPID);//商品
            if (goods != null)
            {
                var MachineOwner = MyDB.S_Store.SingleOrDefault(p => p.SID == goods.SCCLASSID);//机柜所有者
                switch (getStars(MachineOwner.OpenId))
                {
                    case 1:
                        Result = "0.30";
                        FXPrice(order.OopenID, MachineOwner.OpenId, order.OrderID, Convert.ToDecimal(Convert.ToDouble(order.ONumber) * Convert.ToDouble(0.30)), "分享佣金");
                        break;
                    case 2:
                        Result = "0.35";
                        FXPrice(order.OopenID, MachineOwner.OpenId, order.OrderID, Convert.ToDecimal(Convert.ToDouble(order.ONumber) * Convert.ToDouble(0.35)), "分享佣金");
                        break;
                    case 3:
                        Result = "0.40";
                        FXPrice(order.OopenID, MachineOwner.OpenId, order.OrderID, Convert.ToDecimal(Convert.ToDouble(order.ONumber) * Convert.ToDouble(0.40)), "分享佣金");

                        break;
                    case 4:
                        Result = "0.45";
                        FXPrice(order.OopenID, MachineOwner.OpenId, order.OrderID, Convert.ToDecimal(Convert.ToDouble(order.ONumber) * Convert.ToDouble(0.45)), "分享佣金");
                        break;
                    case 5:
                        Result = "0.50";
                        FXPrice(order.OopenID, MachineOwner.OpenId, order.OrderID, Convert.ToDecimal(Convert.ToDouble(order.ONumber) * Convert.ToDouble(0.50)), "分享佣金");
                        break;
                    default: Result = ""; break;
                }
            }

            //只要有订单产生
            //判断该单购买者的上级是不是3级，如果是3级，就给购买者的上级  的上级  100的推广佣金
            var self = MyDB.User.SingleOrDefault(p => p.UGuid == _OPenId).UparentId;//购买者上级ID
            if (!string.IsNullOrEmpty(self))//如果有上级，并且上级达到 3星
            {
                var parent = MyDB.User.SingleOrDefault(p => p.UGuid == self);//购买者的上级
                if (parent != null && (getStars(parent.UGuid) >= 3)) //如果有上级，并且上级达到 3星
                {
                    //给上级  的上级  100的推广佣金
                    var pparent = MyDB.User.SingleOrDefault(p => p.UGuid == parent.UparentId);//购买者  上级的  上级
                    if (pparent != null)
                        FXPrice(parent.UGuid, pparent.UGuid, order.OrderID, Convert.ToDecimal(100), "推广佣金");
                }
            }
        }
        return Result;
    }
    #endregion

    #region 分享
    /// <summary>
    /// 分享//获下级的20%作为“一级分享”，再从这20%中拿出50%作为“一级分红”，一直往上推，直到没有上级或者没钱可分
    /// </summary>
    /// <param name="guid"></param>
    /// <param name="price"></param>
    /// FXPrice(order.OopenID, MachineOwner.OpenId, order.OrderID, Convert.ToDecimal(0.30), "分享佣金");购买商品时给机柜主的
    /// FXPrice(_Uguid, _Uguid, 0, price, "红包")；抢红包hongbao.ashx
    /// methods.FXPrice(mydb.UGuid, parent.UGuid, mydb.CZID, Convert.ToDecimal(100), "推广佣金");充值ResultNotify.cs
    /// FXPrice(parent.UGuid, pparent.UGuid, order.OrderID, Convert.ToDecimal(100), "推广佣金");//重要产生购物
    public static void FXPrice(string shareguid, string guid, int _id, decimal price, string type)
    {
        Entities db = new Entities();
        U_CZ mybm = new U_CZ();
        mybm.UGuid = guid;//享受分红人
        mybm.UShareGuid = shareguid;//谁进行分红
        mybm.Other = _id.ToString();//ID
        mybm.CZMoney = Convert.ToDecimal(price);
        mybm.CZTime = DateTime.Now;
        mybm.CZPayType = type;
        mybm.CZstat = 1;

        db.U_CZ.Add(mybm);
        db.SaveChanges();
    }
    #endregion

    #region 查找第一个达到星级要求的
    public static string getFirstReachStars(string _OPenId)
    {
        string Result = "";
        Dictionary<int, string> PramaryArry = new Dictionary<int, string>();
        Entities MyDB = new Entities();
        ArrayList nextChilds = new ArrayList();//定义一个string的list，用来存放直接下一级OpenId

        var childs = from n in MyDB.User.Where(x => x.UparentId == _OPenId) select n;//所有下级,我的下级
        foreach (var child in childs)
        {
            //-------------横向查找我的下级中，有满足条件的-----------开始--------------//
            var indirentChilds = from n in MyDB.User.Where(x => x.UparentId == child.UOpenID) select n;//下级的下级人数
            // 找到下级人数小等于5人的，结束递归，并返回该用户OPenId；如果不满足找横向的下一个；如果所有横向都不满足，从头

            var btChilds = from n in MyDB.User.Where(x => x.UparentId == child.UOpenID && !string.IsNullOrEmpty(x.UIndirectParentId)) select n;//下级人数中，有几人是帮推的

            if (indirentChilds.Count() < 5 && btChilds.Count() < 1) // 我的下级中人数小等于5人 ， 并且下级中没有是别人帮推的  ，  结束递归
            {
                Result = child.UOpenID;
                break;
            }
            else if (indirentChilds.Count() < 5)  // 我的下级中人数小等于5人 ， 但是下级中有是别人帮推的 ，存入字典，查找帮推人数最少的
            {
                PramaryArry.Add(btChilds.Count(), child.UOpenID);
            }
            //-------------横向查找我的下级中，有满足条件的------------结束--------------//

            //-------------横向查找我的下级中，无满足条件的------------开始--------------//
            else//如果我的下级中，没有满足条件的，，，那么就把我的下级 的下级，存起来  作为下次 横向查找 的User
            {
                foreach (var indirentChild in indirentChilds)//
                {
                    nextChilds.Add(indirentChild.UOpenID);
                }
            }
            //-------------横向查找我的下级中，无满足条件的------------结束--------------//

        }
        if (Result == "") //如果第一级横向未找到 ，递归找第二级横向
        {
            if (PramaryArry.Count() != 0) //如果下级中有没有满5人的，找帮推最少的
            {
                Result = PramaryArry.OrderBy(d => d.Key).Select(d => d.Value).FirstOrDefault();
            }
            else //如果下级的人数全部大等于5人，找下一级
            {
                foreach (var nextchild in nextChilds)
                {
                    getFirstReachStars(nextchild.ToString());
                }
            }

        }
        return Result;
    }
    #endregion

    #region 用户信息
    /// <summary>
    /// 0姓名，1电话，2头像，3所属商家，4已体现，5当日收益，6累计收益，7可提余额，8微信昵称，9星级
    /// </summary>
    /// <param name="_OpenId"></param>
    /// <returns></returns>
    public static string[] getUser(string _OpenId)
    {
        string[] Result = { " ", " ", "", "", "0", "0", "0", "", "0" };
        Entities db = new Entities();
        var _User = db.User.SingleOrDefault(p => p.UOpenID == _OpenId);
        if (_User != null)
        {
            Result = new string[] {
                 string.IsNullOrEmpty(_User.UTrueName) || _User.UTrueName=="null" ?  _User.UNickName : _User.UTrueName,
                 string.IsNullOrEmpty(_User.UTel) ? "" :_User.UTel,
                 string.IsNullOrEmpty(_User.Utouxiang) ?_User.UHeadImg :_User.Utouxiang,
                 "",
                 "0",
                 "0",
                 "0",
                 "0",
                 "",
                 "0"
              };

            //所有的收益:U_CZ表里的所有“分享”和“分红”和 - U_TX表里的体现
            //今日收益:U_CZ表里的今天的“分享”和“分红”

            //“分享”、“分红”“分享”总计，累计收益
            var CZ = db.U_CZ.Where(x => x.UGuid == _OpenId && x.CZstat == 1 && x.CZPayType != "充值").Sum(x => x.CZMoney);


            //“体现”总计
            var TX = db.U_TX.Where(x => x.UGuid == _OpenId).Sum(x => x.TXMoney);      //提现

            if (CZ == null)
                CZ = 0;

            if (TX == null)
                TX = 0;

            DateTime dtToday = DateTime.Now.Date;//今天00:00:00
            var TodaySY = db.U_CZ.Where(x => x.UGuid == _OpenId && x.CZstat == 1 && x.CZTime >= dtToday).Sum(x => x.CZMoney);//当日收益

            Result = new string[] {
                string.IsNullOrEmpty(_User.UTrueName) || _User.UTrueName=="null" ? _User.UNickName : _User.UTrueName,
                 string.IsNullOrEmpty(_User.UTel) ?"" :_User.UTel,
                  string.IsNullOrEmpty(_User.Utouxiang) ?_User.UHeadImg :_User.Utouxiang,
                  "",
                 TX.ToString(),
                 TodaySY.ToString(),
                 CZ.ToString(),
                 (CZ - TX).ToString(),
                 _User.UNickName,
                getStars(_OpenId).ToString()
              };
        }
        return Result;
    }
    #endregion

    #region 获取收益
    /// <summary>
    /// 获取收益：红包，商品（分享佣金），推广佣金。今日收益，总收益
    /// U_CZ.Other 存放 商品ID
    /// </summary>
    /// <param name="OrderId">用户唯一ID</param>
    /// <param name="type">收益类型</param>
    /// <param name="_id">具体设备</param>
    /// <returns></returns>
    /// 通过具体设备ID 找到有那些Order,在算出所有的该OrderID的 商品（分享佣金）
    public static decimal[] getSY(string _OpenId, string type, int _id)
    {
        decimal[] Result = { 0, 0 };
        Entities db = new Entities();
        List<string> myOrders = new List<string>();

        var AllSY = db.U_CZ.Where(x => x.UGuid == _OpenId && x.CZstat == 1 && x.CZPayType == type).Sum(x => x.CZMoney);

        DateTime dtToday = DateTime.Now.Date;//今天00:00:00
        var TodaySY = db.U_CZ.Where(x => x.UGuid == _OpenId && x.CZstat == 1 && x.CZPayType == type && x.CZTime >= dtToday).Sum(x => x.CZMoney);//当日收益

        if (_id != 0) //如果计算的是具体设备的收益
        {
            //1,找出所有OrderID
            var Orders = db.S_Order.Where(s => s.OStoreId == _id).Select(s => new
            {
                s.OrderID
            });
            foreach (var Order in Orders)
            {
                myOrders.Add(Order.OrderID.ToString());
            }
            //2.
            AllSY = db.U_CZ.Where(x => x.UGuid == _OpenId && x.CZstat == 1 && x.CZPayType == type && myOrders.Contains(x.Other)).Sum(x => x.CZMoney);

            TodaySY = db.U_CZ.Where(x => x.UGuid == _OpenId && x.CZstat == 1 && x.CZPayType == type && x.CZTime >= dtToday && myOrders.Contains(x.Other)).Sum(x => x.CZMoney);//当日收益
        }

        if (AllSY == null)
            AllSY = 0;

        Result = new decimal[] { Convert.ToDecimal(TodaySY), Convert.ToDecimal(AllSY) };

        return Result;
    }
    #endregion


    #region 上级信息
    /// <summary>
    /// 上级信息
    /// </summary>
    /// <param name="guid"></param>
    /// <returns></returns>
    public static string getParent(string guid)
    {
        string result = "";
        Entities db = new Entities();
        var LClass = db.User.FirstOrDefault(p => p.UGuid == guid);

        result = "顶级";
        if (!string.IsNullOrEmpty(LClass.UparentId))
        {
            var parent = db.User.FirstOrDefault(p => p.UGuid == LClass.UparentId);
            if (parent != null && !string.IsNullOrEmpty(parent.UTel))
            {
                result = parent.UTrueName + parent.UTel;
            }
            else
            {
                result = "未实名";
            }
        }

        return result;
    }
    #endregion

    #region 下面三级信息
    /// <summary>
    /// 下面三级信息
    /// </summary>
    /// <param name="guid"></param>
    /// <returns></returns>
    public static int[] getChild(string guid)
    {
        int[] result = { 0, 0, 0, 0 };
        Entities MyDB = new Entities();
        var userOne = MyDB.User.Where(x => x.UparentId == guid).OrderByDescending(x => x.UID);//一级
        int sum1 = userOne.Count();

        //二级
        var userAll = MyDB.User;
        var where = PredicateBuilder.False<User>();
        foreach (var n in userOne)
        {
            where = where.Or(x => x.UparentId == n.UGuid);
        }
        var userTwo = userAll.Where(where.Compile()).OrderByDescending(x => x.UID).ToList();
        int sum2 = userTwo.Count();

        //三级
        var where3 = PredicateBuilder.False<User>();
        foreach (var n in userTwo)
        {
            where3 = where3.Or(x => x.UparentId == n.UGuid);
        }
        var userThree = userAll.Where(where3.Compile()).OrderByDescending(x => x.UID).ToList();
        int sum3 = userThree.Count();

        return result = new int[] { sum1, sum2, sum3, sum1 + sum2 + sum3 };
    }
    #endregion

    #region 订单总价
    public static decimal getSumMoney(string OrderId)
    {
        decimal Result = 0;
        int _OrderId = int.Parse(OrderId);
        Entities db = new Entities();
        var mnews = db.S_Order.Where(x => x.OParentId == _OrderId || x.OrderID == _OrderId);
        foreach (var m in mnews)
        {
            Result += Convert.ToDecimal(m.OLastPrice);
        }
        return Result;
    }
    #endregion

    #region 商店信息
    public static string[] getStore(int _SId)
    {
        string[] Result = { " ", " " };
        Entities db = new Entities();

        var store = db.S_Store.SingleOrDefault(p => p.SID == _SId);
        if (store != null)
            Result = new string[] { store.Sname, store.Simg };

        return Result;
    }
    #endregion


    private static Regex RegNumber = new Regex("^[0-9]+$");
    private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
    private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
    private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
    private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串，和 [a-zA-Z0-9] 语法一样 
    private static Regex RegCHZN = new Regex("^[\u4e00-\u9fa5]+$");
    #region  正则表达式匹配中文
    /// <summary>
    /// 正则表达式匹配中文
    /// 知识点：^以中文开头，$以中文结尾，+匹配多个，[\u4e00-\u9fa5]中文ASCII区间
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string GetResultFromReg(string str, Regex Reg)
    {
        string result = "";
        //Regex regex = new Regex("^[\u4e00-\u9fa5]+$");

        for (int i = 0; i < str.Length; i++)
        {
            if (Reg.IsMatch(str[i].ToString()))
            {
                result += str[i];
            }
        }
        return result;
    }
    #endregion

    #region public static string FilterChar(string inputValue) 过滤特殊字符，保留中文，字母，数字，和-
    /// <summary>
    /// 过滤特殊字符，保留中文，字母，数字，和-
    /// </summary>
    /// <param name="inputValue">输入字符串</param>
    /// <remarks>发件和收件详细地址有这种情况：“仓场路40-73号迎园新村四坊69号202室”，这种带有-的特殊字符不需要过滤掉</remarks>
    /// <returns></returns>
    public static string FilterChar(string inputValue)
    {
        return Regex.Replace(inputValue, "[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）&mdash;|{}【】；‘’，。/*-+]+", "", RegexOptions.IgnoreCase);
    }
    #endregion
}